package com.myschool.server.manager.impl;

import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.myschool.common.exception.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.myschool.server.dao.SchoolDao;
import com.myschool.server.dao.UserDao;
import com.myschool.server.dao.support.BaseEntityDaoHibernate;
import com.myschool.server.manager.BaseManager;
import com.myschool.server.manager.MailManager;
import com.myschool.server.manager.UserManager;
import com.myschool.server.model.RegistrationStatus;
import com.myschool.server.model.School;
import com.myschool.server.model.User;
import com.myschool.server.model.UserType;

@Service
@Transactional
public class UserManagerImpl extends BaseManager<User> implements UserManager {

	@Autowired
	private UserDao userDao;

	@Autowired
	private SchoolDao schoolDao;

	@Autowired
	private MailManager mailManager;

	@Override
	protected BaseEntityDaoHibernate getDao() {

		return (BaseEntityDaoHibernate) this.userDao;
	}

	@Transactional(readOnly = true)
	public boolean validateUserLogin(User user) {

		return this.userDao.validateUserLogin(user);

	}

	@Transactional(readOnly = true)
	public User retrieveUser(User user) {

		User persistedUser = this.userDao.retrieveUser(user);

		System.out.println("in user dao");

		return persistedUser;
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Transactional(readOnly = true)
	public void forgotPassword(String email) throws ApplicationException {

		User user = this.userDao.retrieveUserByEmail(email);

		// send email

		// TODO: sending email

	}

	@Transactional(readOnly = false, rollbackFor = ApplicationException.class)
	public void registerUser(User user) throws ApplicationException {

		try {
			School school = this.schoolDao.get(user.getSchool().getEntityId());
			User persistantUser = this.userDao.get(user.getEntityId());
			persistantUser.setType(user.getType());
			persistantUser.setSchool(school);

			persistantUser.setRegistrationStatus(RegistrationStatus.PR);

			this.userDao.save(persistantUser);
			// TODO: send email to admin for registration confirmation
			getMailManager().sendAlertMail("Welcome to myschoollll");
		} catch (Exception exp) {
			throw new ApplicationException(exp,
					"Error during user registration");
		}

	}

	@Transactional(readOnly = false, rollbackFor = ApplicationException.class)
	public void registerSchoolAndUser(User user) throws ApplicationException {

		try {

			this.userDao.save(user);
			String code = RandomStringUtils.randomAlphanumeric(20);

			// TODO: send email to admin for registration confirmation
			// getMailManager().sendAlertMail("Welcome to myschoollll");
			String content = "please click on following link to activate your registration. <br> http://localhost:8080/myschool/register/activateRegistration.htm?user="
					+ user.getEntityId() + "&code=" + code;

			getMailManager().sendMail("admin@myschool.com",
					user.getAddress().getContact().getEmail(),
					"Confirm registration", content);
		} catch (Exception exp) {
			throw new ApplicationException(exp,
					"Error during user registration");
		}

	}

	/**
	 * @return the schoolDao
	 */
	public SchoolDao getSchoolDao() {
		return schoolDao;
	}

	/**
	 * @param schoolDao
	 *            the schoolDao to set
	 */
	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}

	@Transactional(readOnly = true)
	public List<User> retrieveUserByType(UserType userType) {

		List<User> persistedUser = this.userDao.retrieveUserByType(userType);

		System.out.println("in user dao");

		return persistedUser;
	}

	/**
	 * @return the mailManager
	 */
	public MailManager getMailManager() {
		return mailManager;
	}

	/**
	 * @param mailManager
	 *            the mailManager to set
	 */
	public void setMailManager(MailManager mailManager) {
		this.mailManager = mailManager;
	}

	public void activateUser(Long userId, String key) {

		User user = this.userDao.get(userId);

	}
	@Transactional(readOnly = true)
	public List<User> retrieveStudentBySection(Long section) {
		List<User> persistedUser = this.userDao.retrieveStudentBySection(section);
		System.out.println("in user dao");
		return persistedUser;
	}

}
